package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLOutput;
import java.util.Scanner;

/**
 * 自动调用被@注解标注的方法
 */
public class Text {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请出入一个类名：");
        String name = sc.nextLine();
        Class cls = Class.forName(name);

        //  Class cls = Class.forName("reflect.Person");

        //判断当前该类是否被标注
        if(cls.isAnnotationPresent(AutoRunClass.class)){

            //实例化
            Object obj = cls.newInstance();

            //扫描所有方法
            Method [] methods = cls.getDeclaredMethods();

            //遍历所有方法
            for(Method method:methods){

                //判断该方法是否被注解标注
                if(method.isAnnotationPresent(AutoRunMethod.class)){
                    //获取注解参数
                    AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                    int num =arm.value();
                    System.out.println("调用方法："+method.getName()+"()"+num+"次");
                    for (int i = 0; i < num; i++) {
                        method.invoke(obj);
                    }
                }
            }
        }else {
            System.out.println(cls.getSimpleName()+"没有被@AutoRunClass注解标准");
        }
    }
}
